package com.mallcai.bigdata.ladon.utils;

import java.math.BigDecimal;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-06-11 20:16.
 * Modify:
 */
public class DataResultUtils {

    /**
     * 获取BigDecimal值
     *
     * @param obj Object
     * @return BigDecimal
     */
    public static BigDecimal getBigDecimal(Object obj) {
        return getBigDecimal(obj, null);
    }

    public static BigDecimal getBigDecimal(Object obj, BigDecimal defaultVal) {
        if (obj == null) {
            return defaultVal;
        }

        if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        }
        return defaultVal;
    }

    /**
     * 获取String值
     *
     * @param obj Obejct
     * @return String
     */
    public static String getString(Object obj) {
        return getString(obj, null);
    }

    public static String getString(Object obj, String defaultVal) {
        if (obj == null) {
            return defaultVal;
        }
        return String.valueOf(obj);
    }

    /**
     * 获取Integer值
     *
     * @param obj Obejct
     * @return Integer
     */
    public static Integer getInteger(Object obj) {
        return getInteger(obj, null);
    }


    public static Long getLong(Object obj) {
        return getLong(obj, null);
    }

    /**
     * 获取Integer值
     *
     * @param obj Obejct
     * @return Integer
     */
    public static Integer getInteger(Object obj, Integer defaultVal) {
        if (obj == null) {
            return defaultVal;
        }
        try {
            return Integer.parseInt(String.valueOf(obj));
        } catch (Exception e) {
            return defaultVal;
        }
    }

    public static Double getDouble(Object obj, Double defaultVal) {
        if (obj == null) {
            return defaultVal;
        }
        try {
            return Double.parseDouble(String.valueOf(obj));
        } catch (Exception e) {
            return defaultVal;
        }
    }

    public static Long getLong(Object obj, Long defaultVal) {
        if (obj == null) {
            return defaultVal;
        }
        try {
            return Long.parseLong(String.valueOf(obj));
        } catch (Exception e) {
            return defaultVal;
        }
    }

    public static Boolean getBoolean(Object obj, Boolean defaultVal) {
        if (obj == null) {
            return defaultVal;
        }
        try {
            return Boolean.getBoolean(String.valueOf(obj));
        } catch (Exception e) {
            return defaultVal;
        }
    }
}
